package memory;


import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.FlowPane;
import javafx.util.Duration;


public class MemoryUIcontroller {
	private PCB pcb;
	@FXML
	private TextArea textArea;
	@FXML
	private FlowPane flow;

	public void initialize() {
		for (int i = 0; i < 512; i++) {
			Button button = new Button();
			button.setMinHeight(30);
			button.setMinWidth(80);
			flow.getChildren().add(button);
		}
		//createPro();
		//timeline实时刷新
		Timeline timeline = new Timeline();
	     timeline.setCycleCount(Timeline.INDEFINITE);
	  	 timeline.setAutoReverse(true);
	  	 Duration duration = Duration.seconds(1); 
	  	 EventHandler<ActionEvent> onFinished = (ActionEvent r) -> {
	  		update();
			};
		KeyFrame keyFrame = new KeyFrame(duration, onFinished);
		  timeline.getKeyFrames().addAll(keyFrame);   
		  timeline.play();
	}
	
	
	
	public void update() {
		textArea.setText(PCBData.str);
			for(int i = 0;i<PCBData.Blist.size();i++) {
					if (i >= 1)
						for (int k =PCBData.Blist.get(i - 1).getBegin(); k < PCBData.Blist.get(i - 1).getBegin()
								+ PCBData.Blist.get(i).getSize(); k++) {
							Button button = new Button();
							button.setMinHeight(30);
							button.setMinWidth(80);
							if(PCBData.Blist.get(i).isFlag()) {
								if (k == PCBData.Blist.get(i - 1).getBegin())
									button.setText("进程" + PCBData.Blist.get(i).getPid());
								if (k == PCBData.Blist.get(i - 1).getBegin() + 1)
									button.setText(PCBData.Blist.get(i).getSize() + "KB");
								button.setStyle("-fx-background-color: #FF6347;-fx-background-insets: 5;");
								flow.getChildren().set(k, button);
							}
							flow.getChildren().set(k, button);
						}
					if (i == 0)
						for (int k = 0; k < PCBData.Blist.get(i).getSize(); k++) {
							Button button = new Button();
							button.setMinHeight(30);
							button.setMinWidth(80);
							if(PCBData.Blist.get(i).isFlag()) {
								if (k == 0)
									button.setText("进程" + PCBData.Blist.get(i).getPid());
								if (k == 1)
									button.setText(PCBData.Blist.get(i).getSize() + "KB");
								button.setStyle("-fx-background-color: #FF6347;-fx-background-insets: 5;");
								flow.getChildren().set(k, button);
							}
							flow.getChildren().set(k, button);
						}
					}
		}

/*	public void createPro() {
		textArea.setText(PCBData.str);
		for(int n=0;n<PCBData.allPcbList.size();n++) {
		PCB pcb = PCBData.allPcbList.get(n);
		System.out.println(PCBData.allPcbList.get(0).getAddress()+"   "+PCBData.allPcbList.get(0).getSize());
	//	System.out.println(pcb.getAddress()+"///"+pcb.getAddress() + pcb.getSize());
			for (int k = pcb.getAddress(); k < (pcb.getAddress() + pcb.getSize()); k++) {
				Button button = new Button();
				button.setMinHeight(30);
				button.setMinWidth(80);
				if (k == pcb.getAddress())
					button.setText("进程" + pcb.getPid());
				if (k == pcb.getAddress() + 1)
					button.setText(pcb.getSize() + "KB");
				button.setStyle("-fx-background-color: #FF6347;-fx-background-insets: 5;");
				flow.getChildren().set(k, button);
			}
		}
	}*/

	public int searchBlock(int pid) {
		for(int i = 0; i < PCBData.Blist.size(); i++) {
			if (PCBData.Blist.get(i).getPid() == pid) {
				return i;
			}
		}
		return -1;
	}


	public void createAlert(String info) {
		Alert alert = new Alert(Alert.AlertType.INFORMATION);
		alert.setTitle("提示");
		alert.setHeaderText(null);
		alert.setContentText(info);
		alert.showAndWait();

	}

}
